#include "hikari.h"


wstring getAnimationDescriptionFilePath(wstring&path){
	wstring npath=Utils::getFilePath(path)+Utils::getFileNameWithoutExtension(path)+L".anim";
	return npath;
}

AnimationData*loadAnimationData( wstring&filePath){
	HikariFileReader reader(filePath);
	vector<Chunk*>sections=reader.getAllSections();
	AnimationData*animData=new AnimationData();
	for(unsigned int i=0;i<sections.size();i++){
		Chunk*chunk=sections[i];
		if(Utils::equal(chunk->getName(),L"track")){
			FileSection*animTracSection=reader.readSection(chunk);
			int startFrame = animTracSection->readInt(animTracSection->getChunk(L"startFrame"));
            int endFrame = animTracSection->readInt(animTracSection->getChunk(L"endFrame"));
            wstring trackName = animTracSection->readString(animTracSection->getChunk(L"trackName"));
            float sequenceSpeed = animTracSection->readFloat(animTracSection->getChunk(L"speed"));
			delete animTracSection;

			animData->addTrack(trackName, startFrame, endFrame, sequenceSpeed);
		}
	}

	return animData;
}

AnimationData*AnimationManager::getAnimationData(wstring&path){
	long hash=Utils::hash_WString(path);
	AnimationData*data=animationData[hash];
	if(data!=NULL){
		return data;
	}

	/*if(animationData.find(hash)!=animationData.end()){
		return NULL;
	}*/

	wstring filePath=getAnimationDescriptionFilePath(path);
	if(HikariFileReader::isFileExist(filePath)){
		data=loadAnimationData(filePath);
	}
	animationData[Utils::hash_WString(path)]=data;
	return data;
}